import Banner from "../Banner";
import { 头盔, 手枪, 防弹衣, 突击步枪, 冲锋枪, 射手步枪, 栓动步枪, 轻机枪, 霰弹枪, 卡宾枪, 投掷物, 子弹, 药品, 食物, } from "../Datas/Data";
import { Constant, ItemType } from "../Framework/Const/Constant";
import AudioManager from "../Framework/Managers/AudioManager";
import { DataManager } from "../Framework/Managers/DataManager";
import { PoolManager } from "../Framework/Managers/PoolManager";
import PrefsManager from "../Framework/Managers/PrefsManager";
import { UIManager } from "../Framework/Managers/UIManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import SeasonItem from "./SeasonItem";
import ValueBar from "./ValueBar";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SpecialSkinShopPanel extends cc.Component {
    ItemContent: cc.Node = null;
    SeasonButton: cc.Node = null;
    SeasonLvLabel: cc.Label = null;
    ValueBar: ValueBar = null;
    ScrollView: cc.ScrollView = null;

    items: SeasonItem[] = [];

    protected onLoad(): void {
        this.ItemContent = NodeUtil.GetNode("ItemContent", this.node);
        this.SeasonButton = NodeUtil.GetNode("SeasonButton", this.node);
        this.SeasonLvLabel = NodeUtil.GetComponent("SeasonLvLabel", this.node, cc.Label);
        this.ValueBar = NodeUtil.GetComponent("ValueBar", this.node, ValueBar);
        this.ScrollView = NodeUtil.GetComponent("ScrollView", this.node, cc.ScrollView);
    }

    Show() {
        Banner.Instance.ShowCustomAd();
        this.RefreshItems();
        this.RefreshTitle();
        this.SeasonButton.active = !PrefsManager.GetBool(Constant.Key.UnlockSeason);
        this.ScrollView.scrollToPercentHorizontal(DataManager.SeasonLv / 80, 0.1);
    }

    RefreshTitle() {
        this.SeasonLvLabel.string = `${DataManager.SeasonLv}`;
        this.ValueBar.SetAll(DataManager.EXP % 1000 / 1000, `${DataManager.EXP % 1000} /1000`);
    }

    RefreshItems() {
        if (!this.items || this.items.length == 0) {
            const scroll = () => { this.ScrollView.scrollToPercentHorizontal(DataManager.SeasonLv / 80, 0.1) };

            for (let i = 0; i < DataManager.SeasonDatas.length; i++) {
                const data = DataManager.SeasonDatas[i];
                PoolManager.GetNode("Prefabs/UI/SeasonItem", this.ItemContent).then((node: cc.Node) => {
                    let item = node.getComponent(SeasonItem);
                    item.Init(i + 1, data);
                    this.items.push(item);
                    if (i >= DataManager.SeasonDatas.length - 1) scroll();
                });
            }
        } else {
            this.items.forEach(e => e.Refresh());
        }
    }

    OnButtonClick(event: cc.Event) {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);

        switch (event.target.name) {
            case "ReturnButton":
                UIManager.Instance.HidePanel(Constant.Panel.SeasonPanel);
                break;
            case "SeasonButton":
                Banner.Instance.ShowVideoAd(() => {
                    PrefsManager.SetBool(Constant.Key.UnlockSeason, true);
                    this.SeasonButton.active = false;
                    this.RefreshItems();
                    this.RefreshTitle();
                });
                break;
        }
    }
}